const { task } = require("hardhat/config")
const { networkConfig } = require("../helper-hardhat-config")

task("lock-and-cross").addParam("tokenid", "tokenId to be locked and crossed")
    .addOptionalParam("chainselector", "chain selector of destination chain")
    .addOptionalParam("receiver", "receiver in the destination chain")
    .setAction(async(taskArgs, hre) => {
    const tokenId = taskArgs.tokenid

    const { firstAccount } = await getNamedAccounts()
    console.log(`deployer is ${firstAccount}`)

    let destReceiver
    if(taskArgs.receiver) {
        destReceiver = taskArgs.receiver
    } else {
        const nftBurnAndMint = await hre.companionNetworks["destChain"].deployments.get("NFTPoolBurnAndMint")
        destReceiver = nftBurnAndMint.address
    }
    console.log(`NFTPoolBurnAndMint address on destination chain is ${destReceiver}`)

    let destChainSelector
    if(taskArgs.chainselector) {
        destChainSelector = taskArgs.chainselector
    } else {
        destChainSelector = networkConfig[network.config.chainId].companionChainSelector
    }
    console.log(`destination chain selector is ${destChainSelector}`)

    const linkTokenAddr = networkConfig[network.config.chainId].linkToken
    const linkToken = await ethers.getContractAt("LinkToken", linkTokenAddr)
    const nftPoolLockAndRelease = await ethers
        .getContract("NFTPoolLockAndRelease", firstAccount)
    
    // transfer 10 LINK token from deployer to pool
    const balanceBefore = await linkToken.balanceOf(nftPoolLockAndRelease.target)
    console.log(`balance before: ${balanceBefore}`)
    const transferTx = await linkToken.transfer(nftPoolLockAndRelease.target, ethers.parseEther("10"))
    await transferTx.wait(6)
    const balanceAfter = await linkToken.balanceOf(nftPoolLockAndRelease.target)
    console.log(`balance after: ${balanceAfter}`)

    const nft = await ethers.getContract("MyToken", firstAccount)
    await nft.approve(nftPoolLockAndRelease.target, tokenId)
    console.log("approve successfully")

    // ccip send
    console.log(`${tokenId}, ${firstAccount}, ${destChainSelector}, ${destReceiver}`)
    const lockAndCrossTx = await nftPoolLockAndRelease
        .lockAndSendNFT(
        tokenId, 
        firstAccount, 
        destChainSelector, 
        destReceiver
    )
})

module.exports = {}